﻿using System;
using System.Collections.Generic;
using FluentValidation;
using FluentValidation.Results;
using Huirui.Cavan.Core.Cryptography;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;

namespace Huirui.Cavan.Domain.MainBoundedContext.SystemModule
{
    public class SystemModuleFactory
    {
        public static UserAccount CreateUserAccount(
                                string name,
                                string password,
                                string email,
                                DateTime lastActiveTime,
                                bool isLocked,
                                bool isDeleted,
                                decimal creditLimit,
                                Role role,
                                DateTime editTime,
                                int editUser,
                                Employee employee)
        {
            UserAccount userAccount = new UserAccount();
            userAccount.ModifyDate = editTime;
            userAccount.EditUser = editUser;
            userAccount.Email = email;
            userAccount.IsDeleted = isDeleted;
            userAccount.IsLocked = isLocked;
            userAccount.LastActiveTime = lastActiveTime;
            userAccount.Name = name;
            userAccount.Password = Crypto.Encrypt(password);
            userAccount.CreditLimit = creditLimit;
            userAccount.Role = role;
            userAccount.CreateDate = DateTime.Now;
            userAccount.Employee = employee;

            UserAccountProperty userAccountProperty = new UserAccountProperty() { CreateDate = DateTime.Now, EditUser = editUser, ModifyDate = editTime, UserAccount = userAccount };
            userAccount.UserAccountProperty = userAccountProperty;

            return userAccount;
        }

        public static Role CreateRole(
                        string name,
                        string description,
                        bool isUseDenied,
                        DateTime editTime,
                        int editUser)
        {
            Role role = new Role();
            role.Description = description;
            role.ModifyDate = editTime;
            role.EditUser = editUser;
            role.IsUseDenied = isUseDenied;
            role.Name = name;
            role.CreateDate = DateTime.Now;

            return role;
        }

        public static Role CreateRole(int id)
        {
            Role role = new Role();
            role.Id = id;

            return role;
        }

        public static Notice CreateNotice(
                        string title,
                        string titleColor,
                        string content,
                        string link,
                        SystemIdentifier.NoticeType type,
                        int sort,
                        bool isCanShow,
                        int rank,
                        DateTime refreshTime,
                        DateTime editTime,
                        int editUser)
        {
            Notice notice = new Notice();
            notice.Content = content;
            notice.ModifyDate = editTime;
            notice.EditUser = editUser;
            notice.IsCanShow = isCanShow;
            notice.Link = link;
            notice.Rank = rank;
            notice.RefreshTime = refreshTime;
            notice.Sort = sort;
            notice.Title = title;
            notice.TitleColor = titleColor;
            notice.Type = type;
            notice.CreateDate = DateTime.Now;

            return notice;
        }

        public static Area CreateArea(string code, string name, string postCode, int level, string parentCode, string spell, DateTime editTime)
        {
            Area area = new Area();
            area.Id = code;
            area.ModifyDate = editTime;
            area.Level = level;
            area.PostCode = postCode;
            area.ParentCode = parentCode;
            area.Spell = spell;
            area.CreateDate = DateTime.Now;
            return area;
        }

        public static Currency CreateCurrency(string code, string name, decimal rate, bool isLocal, int editUser, Corporation corporation)
        {
            Currency currency = new Currency(code, name, rate, isLocal, DateTime.Now, DateTime.Now, editUser, corporation);

            return currency;
        }

        public static Corporation CreateCorporation(string code, string name, IndustryType industry, string englishName, int maxUser, string contact, string website, string mobile, string telephone, string fax, string email, Area area, string address, string addressEnglish, string deliveryAddress2, string deliveryAddress3, string deliveryAddress1, string taxIdentificationNumber, string bank, string bankAccount, int editUser)
        {
            Corporation corporation = new Corporation(code, name, industry, englishName, maxUser, contact, website, mobile, telephone, fax, email, area, address, addressEnglish,
                deliveryAddress1, deliveryAddress2, deliveryAddress3, taxIdentificationNumber, bank, bankAccount, DateTime.Now, DateTime.Now, editUser);

            return corporation;
        }

        public static Department CreateDepartment(string code, string name, string remark, string superiorDepartment, int editUser, Corporation corporation)
        {
            Department department = new Department(code, name, remark, superiorDepartment, DateTime.Now, DateTime.Now, editUser, corporation);

            return department;
        }

        public static Employee CreateEmployee(string code, string name, Job job, decimal? commissionRate, SystemIdentifier.Sex sex, DateTime? birthday, string email, string mobile, bool sMSQueryEnabled, string remark, string managerEmployeeCode, int editUser, Corporation corporation, Department department, UserAccount userAccount)
        {
            Employee employee = new Employee(code, name, job, commissionRate, sex, birthday, email, mobile, sMSQueryEnabled, remark, managerEmployeeCode,
                DateTime.Now, DateTime.Now, editUser, corporation, department, userAccount);

            EmployeeValidator validator = new EmployeeValidator();
            ValidationResult results = validator.Validate(employee);
            if (!results.IsValid)
            {
                throw new ValidationException(results.Errors);
            }

            return employee;
        }

        public static Customer CreateCustomer(string id, SystemIdentifier.AgentType agentType, string name, SystemIdentifier.CustomerNature nature, string deliveryAddress, string contact, string telephone, string mobile, string fax, string email, Area area, string address, string addressEnglish, string deliveryAddress1, string deliveryAddress2, string postCode, SystemIdentifier.PayType payType,
            decimal discountRate, int payDay, decimal creditLimit, string taxIdentificationNumber, SystemIdentifier.InvoiceType invoiceType,
            string bank, string bankAccount, string remark, int editUser, Role employeeRole, Corporation corporation, WareHouse wareHouse)
        {
            Customer customer = new Customer(id, agentType, name, nature, deliveryAddress, contact, telephone, mobile, fax, email, area,
                address, addressEnglish, deliveryAddress1, deliveryAddress2, postCode, payType, discountRate, payDay,
                creditLimit, taxIdentificationNumber, invoiceType, bank, bankAccount, remark, true,
                DateTime.Now, DateTime.Now, editUser, employeeRole, corporation, wareHouse);

            return customer;
        }

        public static Supplier CreateSupplier(string id, SystemIdentifier.SupplierNature nature, string name, SupplierType type, string contact, string telephone, string mobile, string fax, string email, Area area, string address, string addressEnglish, string deliveryAddress1, string deliveryAddress2, SystemIdentifier.PayType payType, decimal discountRate, int payDay, decimal creditLimit, decimal defaultTaxRate, string remark, int editUser, Role employeeRole, Corporation corporation)
        {
            Supplier supplier = new Supplier(id, nature, name, type, contact, telephone, mobile, fax, email, area, address,
                addressEnglish, deliveryAddress1, deliveryAddress2, payType, discountRate, payDay, creditLimit, defaultTaxRate,
                remark, DateTime.Now, DateTime.Now, editUser, employeeRole, corporation);

            SupplierValidator validator = new SupplierValidator();
            ValidationResult results = validator.Validate(supplier);
            if (!results.IsValid)
            {
                throw new ValidationException(results.Errors);
            }

            return supplier;
        }

        public static BaseCodeRule CreateBaseCodeRule(string name, SystemIdentifier.BaseCodeRuleType type, string prefix, bool isDefault, int serialNumberNumber, int serialNumberStart, string sample, int editUser, Corporation corporation)
        {
            BaseCodeRule baseCodeRule = new BaseCodeRule(name, type, prefix, isDefault, serialNumberNumber, serialNumberStart, sample, DateTime.Now,
                DateTime.Now, editUser, corporation);

            return baseCodeRule;
        }

        public static ReceiptCodeRule CreateReceiptCodeRule(string name, SystemIdentifier.ReceiptCodeRuleType type, string prefix, SystemIdentifier.CodePattern pattern, bool isDefault, int serialNumberNumber, int serialNumberStart, int yearNumber, string example, int editUser, Corporation corporation)
        {
            ReceiptCodeRule receiptCodeRule = new ReceiptCodeRule(name, type, prefix, pattern, isDefault, serialNumberNumber, serialNumberStart,
                yearNumber, example, DateTime.Now, DateTime.Now, editUser, corporation);

            return receiptCodeRule;
        }

        public static Project CreateProject(string name, decimal budgetIncome, decimal budgetCost, DateTime startDate, DateTime endDate, string remark, int editUser, Corporation corporation)
        {
            Project project = new Project(name, budgetIncome, budgetCost, startDate, endDate, remark, DateTime.Now, DateTime.Now, editUser, corporation);

            return project;
        }

        public static Module CreateModule(string name, int editUser)
        {
            Module module = new Module(name, DateTime.Now, DateTime.Now, editUser);

            return module;
        }

        public static Resource CreateResource(string name, int editUser, Module module)
        {
            Resource resource = new Resource(name, DateTime.Now, DateTime.Now, editUser, module);

            return resource;
        }

        public static Account CreateAccount(string id, string name, string currency, SystemIdentifier.AccountType type, SystemIdentifier.AccountStatus status, int editUser, Corporation corporation)
        {
            Account account = new Account(id, name, currency, type, status, DateTime.Now, DateTime.Now, editUser, corporation);

            return account;
        }

        public static FeeType CreateFeeType(string name, string value, DateTime createDate, DateTime modifyDate, int editUser, Corporation corporation)
        {
            FeeType feeType = new FeeType(name, value, DateTime.Now, DateTime.Now, editUser, corporation);

            return feeType;
        }

        public static SystemConfig CreateSystemConfig(SystemIdentifier.SystemConfigType type, string item, string value, SystemIdentifier.DataType dataType, string description, string remark, int editUser, Corporation corporation)
        {
            SystemConfig systemConfig = new SystemConfig(type, item, value, dataType, description, remark, DateTime.Now, DateTime.Now, editUser, corporation);

            return systemConfig;
        }

        public static CodeRuleProperty CreateCodeRuleProperty(int codeRuleID, int codeRuleType, long max, int editUser)
        {
            CodeRuleProperty codeRuleProperty = new CodeRuleProperty(codeRuleID, codeRuleType, max, DateTime.Now, DateTime.Now, editUser);

            return codeRuleProperty;
        }

        public static Job CreateJob(string name, string remark, int editUser, Corporation corporation)
        {
            Job job = new Job(name, remark, DateTime.Now, DateTime.Now, editUser, corporation);

            return job;
        }
    }
}